/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef HTTP2_HTTP2_STRUCT_H
#define HTTP2_HTTP2_STRUCT_H
#include <nghttp2/nghttp2.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/conf.h>
#include <poll.h>
#include <string>
#include "http2_constant.h"
#include "napi/native_api.h"
using namespace std;

struct Connection {
    SSL *ssl = nullptr;
    nghttp2_session *session = nullptr;
    int wantIo;
    SSL_CTX *sslCtx = nullptr;
    int streamId = -1;
    int fd;
    int writeBuffLen;
    napi_env env;
    napi_async_work asyncWork;
    napi_deferred deferred;
    napi_ref callbackRef;
    string host = "";
    string path = "";
    string reqId = "";
    nghttp2_session_callbacks *callbacks;
    vector<uint8_t> responseData;
    bool canSendData = false;
    uint8_t *buffer;
    bool dataFlags = false; // true EOF
    int rpcType = 1; //1 Unary 2 WritableStream 3 ReadableStream 4 DuplexStream
    string ca = "";
    string cert = "";
    string key = "";
    bool recvLargeDataFlags = false; // true Receive large data
};


struct RequestTask {
    napi_env env;
    napi_async_work asyncWork;
    napi_deferred deferred;
    napi_ref callbackRef;
    int fd = 0;
    string host = "";
    string path = "";
    string port = "";
    string extendParams;
    int errCode;
    void* buff;
    int buffLen;
    string dnsText = "";
    napi_value promise;
    string reqId = "";
    int rpcType = 1; //1 Unary 2 WritableStream 3 ReadableStream 4 DuplexStream
    string ca = "";
    string cert = "";
    string key = "";
};

struct RequestCallback {
    napi_env env;
    napi_async_work asyncWork;
    napi_deferred deferred;
    napi_ref callbackRef;
    int type;
    void *data = nullptr;
    int errorCode;
    string reqId = "";
    int dataLen;
    int rpcType = 1; //1 Unary 2 WritableStream 3 ReadableStream 4 DuplexStream
};

struct HttpStreamErrorCallback {
    int errCode;
    const char *msg;
};

struct HandleIoResult {
    bool isRecv;
    int code;
};

struct Message {
    int type;
    std::shared_ptr<char[]> data;
    int len;
    int rpcType = 1; //1 Unary 2 WritableStream 3 ReadableStream 4 DuplexStream
    string reqId = "";
};

#endif // HTTP2_HTTP2_STRUCT_H
